package com.hontech.desktoppastacooker.utils

import okhttp3.internal.notify
import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.ReentrantLock

class SyncValue<T> {

    private var value: T? = null
    private val lock = ReentrantLock()
    private val cond = lock.newCondition()

    fun reset() {
        value = null
    }

    fun await(timeout: Long): T? {
        lock.lock()
        do {
            if (value != null) {
                break
            }
            cond.await(timeout, TimeUnit.MILLISECONDS)
        } while (false)
        val ret = value
        value = null
        lock.unlock()
        return ret
    }

    fun signal(v: T) {
        lock.lock()
        value = v
        cond.signal()
        lock.unlock()
    }

}